/**
 * 公用方法
 * Created by zhangluxin on 16/8/4.
 */

/**
 * 判断是不是空字符串
 * @param {String} str
 * @returns {Boolean}
 */
var isNullString = function (str) {
    return !_.isString(str) || str === '' || str === 'null';
};

var CommonUtil = CommonUtil || {};

CommonUtil.crash = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('CommonUtil', 'crash');
    } else {
        cc.director.end();
        cc.director.popScene();
        cc.director.popScene();
        cc.director.purgeDirector();
    }
};

CommonUtil.getBundleId = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('CommonUtil', 'getBundleId');
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getBundleId', '()Ljava/lang/String;');
    }
    return '';
};

CommonUtil.getUdid = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('CommonUtil', 'getUdid');
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/CommonUtil', 'fetchUDID', '()Ljava/lang/String;');
    }
    return '';
};

CommonUtil.fetchUDID = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('CommonUtil', 'fetchUDID');
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/CommonUtil', 'fetchUDID', '()Ljava/lang/String;');
    }
    return '';
};

CommonUtil.getVersionName = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('CommonUtil', 'getVersionName');
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/Global', 'getVersionName', '()Ljava/lang/String;');
    }
    return '';
};

CommonUtil.savePasteBoard = function (text) {
    if (cc.sys.os === cc.sys.OS_IOS) {
        if(window.nativeVersion <'3.0.0'){
            jsb.reflection.callStaticMethod("AppController", "savePasteBoard:", text);
        }else{
            jsb.reflection.callStaticMethod('CommonUtil', 'savePasteBoard:', text);
        }
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/ClipboardUtil', 'clipboardCopyText', '(Ljava/lang/String;)V', text);
    }
};

CommonUtil.getPasteBoard = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('CommonUtil', 'getPasteBoard');
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/ClipboardUtil', 'getPasteBoard', '()Ljava/lang/String;');
    }
    return '';
};

CommonUtil.openApp = function (app) {
    if (typeof app === 'undefined') {
        app = cc.sys.os === cc.sys.OS_IOS ? 'weixin://' : 'com.tencent.mm';
    }
    if (cc.sys.os === cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('CommonUtil', 'openApp:', app);
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/CommonUtil', 'openApp', '(Ljava/lang/String;)Z', app);
    }
};

CommonUtil.getBatteryLevel = function () {
    try {
        if (cc.sys.os === cc.sys.OS_IOS) {
            return jsb.reflection.callStaticMethod('CommonUtil', 'getBatteryLevel');
        } else if (cc.sys.os === cc.sys.OS_ANDROID) {
            return jsb.reflection.callStaticMethod(packageUri + '/utils/CommonUtil', 'getBatteryLevel', '()Ljava/lang/String;');
        }
    }catch (e) {
    }
    return '100';
};

CommonUtil.getBatteryInfo = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('CommonUtil', 'getBatteryInfo');
    }
    return '';
};

CommonUtil.getNextIp = function (app_key, group) {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod(
            'CommonUtil',
            'ni2:n:',
            app_key,
            group
        );
    }
    else {
        return jsb.reflection.callStaticMethod(
            'org/cocos2dx/javascript/AppActivity',
            'ni2',
            '(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;',
            app_key,
            group
        );
    }
};


/**
 * 截图
 * @param {cc.Node} node
 * @param {Function} callback
 * @public
 */
CommonUtil.captureNode = function (node, callback) {
    if (!cc.sys.isNative) {
        return;
    }
    var texture = new cc.RenderTexture(cc.winSize.width, cc.winSize.height, null, 0x88F0);
    texture.retain();
    texture.setAnchorPoint(0, 0);
    texture.begin();
    node.visit();
    texture.end();
    var time = timestamp2time(Math.round((new Date()).valueOf() / 1000));
    time = time.replace(/[\s:-]+/g, '_');
    var nameJPG = 'ss-' + time + '.jpg';

    texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, false, function () {
        texture.release();
        callback(jsb.fileUtils.getWritablePath() + nameJPG);
    });
};